<?php
require_once('../config/config.php');
require_once(FQ_THIRDPARTY.'/adodb5/adodb.inc.php');
require_once(FQ_LIB.'/DBUtils.php');
require_once(FQ_LIB.'/SessionManager.php');
require_once(FQ_LIB.'/UAC.php');
require_once(FQ_LIB.'/Logger.class.php');
require_once(FQ_ROOT .'/modules/base/flexqueries_base.class.php');
require_once(FQ_ROOT .'/modules/base/templateengine_base.class.php');

try
{
	SessionManager::loadFromCookie();
	//Access control object
	$uac = new UAC(SessionManager::$instance);
	
	if(empty($_REQUEST['module']))
		$_REQUEST['module'] = FQ_DEFAULT_MODULE;

	if(empty($_REQUEST['action']))
		$_REQUEST['action'] = 'home';
		
	$module = $_REQUEST['module'];
	$action = $_REQUEST['action'];

	//Check user access
	if(UAC_ENABLED)
	{
		if(!$uac->isActionAllowed($module, $action))
			throw new Exception("$module.$action not allowed");
	}
	
	$moduleFileName = realpath(FQ_ROOT ."/modules/$module.class.php");

	if(!$moduleFileName || !file_exists($moduleFileName))
		throw new Exception("Unable to initialize module");
		
	require_once(FQ_ROOT ."/modules/$module.class.php");
	
	//Instantiate the module object
	$obj = new $module();
		
	$method = $action."_action";

	if(!method_exists($obj, $method))
		throw new Exception("Action unavailable");
	
	//Calls the action method
	$output = $obj->$method();
	
	//if the output is empty, we just generate a clean empty page:
	if(empty($output))
		$output = TemplateEngine_base::generateMessage('Nothing to say...');
	
}
catch(Exception $e)
{
	/*header('HTTP/1.1 500 Internal Server Error');
	echo "Stop !";
	exit;*/
	//Shows the error in a beautiful way
	//$output = $output = TemplateEngine_base::generateMessage($e->getMessage());
	echo $e->getMessage();
	exit;
	$output = TemplateEngine_base::generateMessage($e->getMessage());
	
	
	//print_r($e->getTrace());
}

header('Content-Type: text/html');
echo $output;

?>